package com.cloud.servlet;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.cloud.core.BaseServlet;
import com.cloud.dao.AppointmentBean;
import com.cloud.service.AppointmentService;
import com.cloud.util.Code;
import com.cloud.util.CommUtil;

/**
 * 1Cloud1
 * @author Administrator
 * @since 2016-1-19
 * @version 1.0 
 */
@Controller
@RequestMapping("/appointment")
public class AppointmentServlet extends BaseServlet {
	@Autowired
	private AppointmentService AppointmentService;
	
	/**
	 * 获取工作时间
	 * @param request
	 * @param docid
	 * @param week
	 * @return
	 */
	@RequestMapping(value = "/{docid}/{week}/clinic", method = { RequestMethod.GET})
	public @ResponseBody Object cliniclist(HttpServletRequest request, @PathVariable String docid, @PathVariable String week) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommUtil.isEmpty(docid) || CommUtil.isEmpty(week)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		AppointmentBean bean = new AppointmentBean();
		bean.setDoc_id(docid);
		if(!"all".equalsIgnoreCase(week)){
			bean.setWeeks(week);
		}
		List<AppointmentBean> retAppointmentList = AppointmentService.getClinicList(bean);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", retAppointmentList);
		return map;
	}
	
	/**
	 * 添加工作时间
	 * @param request
	 * @param appointment
	 * @return
	 */
	@RequestMapping(value = "/add/clinic", method = { RequestMethod.POST})
	public @ResponseBody Object addclinic(HttpServletRequest request, @RequestBody AppointmentBean appointment) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(appointment == null || CommUtil.isEmpty(appointment.getDoc_id()) || CommUtil.isEmpty(appointment.getWeeks()) 
				|| (CommUtil.isEmpty(appointment.getM_from())&&CommUtil.isEmpty(appointment.getM_to())&&CommUtil.isEmpty(appointment.getAf_from())&&CommUtil.isEmpty(appointment.getAf_to()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		AppointmentService.addClinic(appointment);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 修改工作时间
	 * @param request
	 * @param appointment
	 * @return
	 */
	@RequestMapping(value = "/update/clinic", method = { RequestMethod.POST})
	public @ResponseBody Object aupdateclinic(HttpServletRequest request, @RequestBody AppointmentBean appointment) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(appointment == null || CommUtil.isEmpty(appointment.getDoc_id()) || CommUtil.isEmpty(appointment.getWeeks()) 
				|| (CommUtil.isEmpty(appointment.getM_from())&&CommUtil.isEmpty(appointment.getM_to())&&CommUtil.isEmpty(appointment.getAf_from())&&CommUtil.isEmpty(appointment.getAf_to()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		AppointmentService.updateClinic(appointment);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 删除工作时间
	 * @param request
	 * @param appointment
	 * @return
	 */
	@RequestMapping(value = "/del/clinic", method = { RequestMethod.POST})
	public @ResponseBody Object delclinic(HttpServletRequest request, @RequestBody AppointmentBean appointment) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(appointment == null || CommUtil.isEmpty(appointment.getDoc_id())){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		AppointmentBean bean = new AppointmentBean();
		bean.setDoc_id(appointment.getDoc_id());
		if(!CommUtil.isEmpty(appointment.getWeeks())){
			bean.setWeeks(appointment.getWeeks());
		}
		AppointmentService.deleteClinic(bean);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 医生获取预约
	 * @param request
	 * @param docid
	 * @param week
	 * @return
	 */
	@RequestMapping(value = "/{docid}/{patid}/{status}/order", method = { RequestMethod.GET})
	public @ResponseBody Object orderclist(HttpServletRequest request, @PathVariable String docid, @PathVariable String patid, @PathVariable String status, String month) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommUtil.isEmpty(docid) || CommUtil.isEmpty(patid) || CommUtil.isEmpty(status)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		AppointmentBean bean = new AppointmentBean();
		bean.setDoc_id(docid);
		if(!CommUtil.isEmpty(month) && month.length() == 6){
			bean.setCreatetime(month);
			//bean.setCreatetime(new SimpleDateFormat("yyyyMM").format(new Date()));
		}
		if(!"all".equalsIgnoreCase(patid)){
			bean.setPat_id(patid);
		}
		if(!"all".equalsIgnoreCase(status)){
			bean.setStatus(status);
		}
		List<AppointmentBean> retAppointmentList = AppointmentService.getAppointmentList(bean);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", retAppointmentList);
		return map;
	}
	
	/**
	 * 患者获取预约
	 * @param request
	 * @param docid
	 * @param week
	 * @return
	 */
	@RequestMapping(value = "/{patid}/{status}/my/order", method = { RequestMethod.GET})
	public @ResponseBody Object orderplist(HttpServletRequest request, @PathVariable String patid, @PathVariable String status, String docid, String month) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(CommUtil.isEmpty(patid) || CommUtil.isEmpty(status)){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		AppointmentBean bean = new AppointmentBean();
		bean.setPat_id(patid);
		if(!CommUtil.isEmpty(month) && month.length() == 6){
			//bean.setCreatetime(new SimpleDateFormat("yyyyMM").format(new Date()));
			bean.setCreatetime(month);
		}
		if(!CommUtil.isEmpty(docid)){
			bean.setDoc_id(docid);
		}
		if(!"all".equalsIgnoreCase(status)){
			bean.setStatus(status);
		}
		List<AppointmentBean> retAppointmentList = AppointmentService.getAppointmentList(bean);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		map.put("data", retAppointmentList);
		return map;
	}
	
	/**
	 * 病人预约
	 * @param request
	 * @param appointment
	 * @return
	 */
	@RequestMapping(value = "/add/order", method = { RequestMethod.POST})
	public @ResponseBody Object addorder(HttpServletRequest request, @RequestBody AppointmentBean appointment) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(appointment == null || CommUtil.isEmpty(appointment.getDoc_id()) || CommUtil.isEmpty(appointment.getPat_id()) 
				|| (CommUtil.isEmpty(appointment.getM_from())&&CommUtil.isEmpty(appointment.getM_to())&&CommUtil.isEmpty(appointment.getAf_from())&&CommUtil.isEmpty(appointment.getAf_to()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		AppointmentService.addAppointment(appointment);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 医生处理预约
	 * @param request
	 * @param appointment
	 * @return
	 */
	@RequestMapping(value = "/update/order", method = { RequestMethod.POST})
	public @ResponseBody Object aupdateorder(HttpServletRequest request, @RequestBody AppointmentBean appointment) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(appointment == null || CommUtil.isEmpty(appointment.getStatus()) || CommUtil.isEmpty(appointment.getDoc_id()) || CommUtil.isEmpty(appointment.getPat_id()) 
				|| (CommUtil.isEmpty(appointment.getM_from())&&CommUtil.isEmpty(appointment.getM_to())&&CommUtil.isEmpty(appointment.getAf_from())&&CommUtil.isEmpty(appointment.getAf_to()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		if("1".equals(appointment.getStatus()) || "2".equals(appointment.getStatus())){
			AppointmentService.updateAppointment(appointment);
		}
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
	
	/**
	 * 取消预约
	 * @param request
	 * @param appointment
	 * @return
	 */
	@RequestMapping(value = "/del/order", method = { RequestMethod.POST})
	public @ResponseBody Object delorder(HttpServletRequest request, @RequestBody AppointmentBean appointment) {
		Map<String, Object> map = new HashMap<String, Object>();
		if(appointment == null || CommUtil.isEmpty(appointment.getDoc_id()) || CommUtil.isEmpty(appointment.getPat_id()) 
				|| (CommUtil.isEmpty(appointment.getM_from())&&CommUtil.isEmpty(appointment.getM_to())&&CommUtil.isEmpty(appointment.getAf_from())&&CommUtil.isEmpty(appointment.getAf_to()))){
			map.put("code", Code.C400.key);
			map.put("msg", Code.C400.desc);
			return map;
		}
		boolean isDeal = false;
		List<AppointmentBean> retAppointmentList = AppointmentService.getAppointmentList(appointment);
		for(AppointmentBean bean : retAppointmentList){
			if(!"0".equals(bean.getStatus())){
				isDeal = true;
				break;
			}
		}
		if(isDeal){
			map.put("code", Code.C450.key);
			map.put("msg", Code.C450.desc);
			return map;
		}
		AppointmentService.deleteAppointment(appointment);
		map.put("code", Code.C200.key);
		map.put("msg", Code.C200.desc);
		return map;
	}
}
